package com.rfid.acs;

import android.text.TextUtils;

import com.rfid.acs.bean.BookBean;
import com.rfid.acs.net.bean.ReaderInfoBean;
import com.rfid.acs.net.tcp.FiledId;

import java.util.ArrayList;
import java.util.List;

class  SearchReaderResponseBean extends AcsResponseBean {

    /**
     * 设备编号
     */
    @FiledId(name = "AO")
    private String devicesNo;

    /**
     * 读者证号
     */
    @FiledId(name = "AA")
    private String readerNo;

    /**
     * 读者名字
     */
    @FiledId(name = "AE")
    private String name;

    /**
     * 最大可借数，取值范围0000-9999
     */
    @FiledId(name = "BZ")
    private String bookLimit;

    /**
     * 验证读者：Y表示读者存在，N表示读者不存在
     */
    @FiledId(name = "BL")
    private String verifyReader;

    /**
     * 验证密码：Y表示读者密码正确，N表示读者密码错误
     */
    @FiledId(name = "CQ")
    private String verifyPassword;

    /**
     * 读者欠费金额
     */
    @FiledId(name = "CC")
    private String arrearage;

    /**
     * 读者押金
     */
    @FiledId(name = "BV")
    private String pledge;

    /**
     * 读者预付款
     */
    @FiledId(name = "YF")
    private String advance;

    /**
     * 当前借阅图书条码
     */
    @FiledId(name = "AS")
    private String currentNo;

    /**
     * 当前超期图书条码
     */
    @FiledId(name = "AT")
    private String extendedNo;

    /**
     * 读者类型
     */
    @FiledId(name = "LX")
    private String readerType;

    /**
     * 地址
     */
    @FiledId(name = "BD")
    private String address;

    /**
     * 邮箱
     */
    @FiledId(name = "BE")
    private String email;

    /**
     * 电话
     */
    @FiledId(name = "BF")
    private String phoneNumber;

    /**
     * 身份证
     */
    @FiledId(name = "XO")
    private String idCard;

    /**
     * 生日
     */
    @FiledId(name = "XH")
    private String birth;

    /**
     * 办证日期
     */
    @FiledId(name = "XE")
    private String createCardDate;

    /**
     * 证过期日期
     */
    @FiledId(name = "XD")
    private String extendedDate;

    /**
     * 提示信息
     */
    @FiledId(name = "AF")
    private String hintMessage;

    /**
     * 所借书籍数量(AS字段书籍最多不超过XX本,超过XX本以后书籍数量以BC字段为准)
     */
    @FiledId(name = "BC")
    private String bookCount;


    /**
     * 打印信息
     */
    @FiledId(name = "AG")
    private String printMessage;

    public String getDevicesNo() {
        return devicesNo;
    }

    public void setDevicesNo(String devicesNo) {
        this.devicesNo = devicesNo;
    }

    public String getReaderNo() {
        return readerNo;
    }

    public void setReaderNo(String readerNo) {
        this.readerNo = readerNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBookLimit() {
        return bookLimit;
    }

    public String getBookCount() {
        return bookCount;
    }

    public void setBookCount(String bookCount) {
        this.bookCount = bookCount;
    }

    public void setBookLimit(String bookLimit) {
        this.bookLimit = bookLimit;
    }

    public String getVerifyReader() {
        return verifyReader;
    }

    public void setVerifyReader(String verifyReader) {
        this.verifyReader = verifyReader;
    }

    public String getVerifyPassword() {
        return verifyPassword;
    }

    public void setVerifyPassword(String verifyPassword) {
        this.verifyPassword = verifyPassword;
    }

    public String getArrearage() {
        return arrearage;
    }

    public void setArrearage(String arrearage) {
        this.arrearage = arrearage;
    }

    public String getPledge() {
        return pledge;
    }

    public void setPledge(String pledge) {
        this.pledge = pledge;
    }

    public String getAdvance() {
        return advance;
    }

    public void setAdvance(String advance) {
        this.advance = advance;
    }

    public String getCurrentNo() {
        return currentNo;
    }

    public void setCurrentNo(String currentNo) {
        this.currentNo = currentNo;
    }

    public String getExtendedNo() {
        return extendedNo;
    }

    public void setExtendedNo(String extendedNo) {
        this.extendedNo = extendedNo;
    }

    public String getReaderType() {
        return readerType;
    }

    public void setReaderType(String readerType) {
        this.readerType = readerType;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }

    public String getCreateCardDate() {
        return createCardDate;
    }

    public void setCreateCardDate(String createCardDate) {
        this.createCardDate = createCardDate;
    }

    public String getExtendedDate() {
        return extendedDate;
    }

    public void setExtendedDate(String extendedDate) {
        this.extendedDate = extendedDate;
    }

    public String getHintMessage() {
        return hintMessage;
    }

    public void setHintMessage(String hintMessage) {
        this.hintMessage = hintMessage;
    }

    public String getPrintMessage() {
        return printMessage;
    }

    public void setPrintMessage(String printMessage) {
        this.printMessage = printMessage;
    }

    public SearchReaderResponseBean(String source) {
        super(source);
    }

    @Override
    protected int headerLength() {
        return 61;
    }

    @Override
    public boolean isSuccess() {
        if ("读者过期借阅".equals(hintMessage)) {
            return false;
        }
        return "Y".equals(verifyPassword) && "Y".equals(verifyReader);
    }

    public ReaderInfoBean convertToReaderBean() {
        ReaderInfoBean bean = new ReaderInfoBean();
        bean.setName(getName());
        bean.setReaderNo(getReaderNo());
        bean.setVerifyReader("Y".equals(getVerifyReader()));
        bean.setVerifyPassword("Y".equals(getVerifyPassword()));
        bean.setSuccess(isSuccess());
        bean.setBookLimit(getBookLimit());
        bean.setBookCount(getBookCount());
        bean.setHintMessage(getHintMessage());
        List<String> allBookNos = new ArrayList<>();
        List<BookBean> allBooks = new ArrayList<>();

        if (!TextUtils.isEmpty(currentNo)) {
            String[] split = currentNo.split(",");

            for (String str : split) {
                if (!TextUtils.isEmpty(str)) {
                    String[] bookSplit = str.split("#");
                    //添加BOOKNO
                    if (bookSplit.length > 0) {
                        allBookNos.add(bookSplit[0]);
                    }
                    //添加book
                    BookBean book = buildBookBean(bookSplit);
                    if (book != null) {
                        allBooks.add(book);
                    }
                }
            }
        }

        if (!TextUtils.isEmpty(extendedNo)) {
            String[] split = extendedNo.split(",");

            for (String str : split) {
                if (!TextUtils.isEmpty(str)) {
                    String[] bookSplit = str.split("#");
                    //添加BOOKNO
                    if (bookSplit.length > 0) {
                        allBookNos.add(bookSplit[0]);
                    }
                    //添加book
                    BookBean book = buildBookBean(bookSplit);
                    if (book != null) {
                        allBooks.add(book);
                    }
                }
            }
        }

        bean.setAllBookNos(allBookNos);
        bean.setAllBooks(allBooks);
        return bean;
    }

    private BookBean buildBookBean(String[] bookInfo) {
        if (bookInfo != null && bookInfo.length > 0) {
            BookBean bean = new BookBean();
            bean.setBookId(bookInfo.length > 0 ? bookInfo[0] : "");
            bean.setBookName(bookInfo.length > 1 ? bookInfo[1] : "");
            bean.setBorrowDate(bookInfo.length > 2 ? bookInfo[2] : "");
            bean.setLimitDate(bookInfo.length > 3 ? bookInfo[3] : "");
            return bean;
        }
        return null;
    }

}
